<?php

class CourseItemBean implements IDBBean {
	public $courseName;
	public $teacherName;
	public $credential;
	public $unit;
	public $courseBegin;
	public $courseEnd;
	public $salaryStandard;
	public $salaryFix;
	public $checkTime;
	public $weekCount;
	public $monthUnit;
	public $preSalary;
	
	public function __construct(array $fields = array()) {
		if (is_array ( $fields )) {
			foreach ( $fields as $key => $value ) {
				$this->$key = $value;
			}
			$this->weekCount = $this->calculateWeekCount ();
			$this->monthUnit = $this->weekCount * $this->unit;
			$this->preSalary = $this->calculatePreSalary ();
		}
	}
	
	private function calculateWeekCount() {
		$preCourseBegin = substr ( $this->courseBegin, 0, 7 );
		$preCourseEnd = substr ( $this->courseEnd, 0, 7 );
		if ($preCourseBegin == $this->checkTime && $preCourseEnd == $this->checkTime) {
			$begin = strtotime ( $this->courseBegin );
			$end = strtotime ( $this->courseEnd );
		} else if ($preCourseBegin == $this->checkTime && $preCourseEnd != $this->checkTime) {
			$begin = strtotime ( $this->courseBegin );
			$end = strtotime ( $this->checkTime . '-' . date ( 't', strtotime ( $this->checkTime ) ) );
		} else if ($preCourseBegin != $this->checkTime && $preCourseEnd == $this->checkTime) {
			$begin = strtotime ( $this->checkTime . '-01' );
			$end = strtotime ( $this->courseEnd );
		} else if ($preCourseBegin < $this->checkTime && $preCourseEnd > $this->checkTime) {
			$s = $this->checkTime . '-01';
			$s2 = $this->checkTime . '-' . date ( 't', strtotime ( $this->checkTime ) );
			$begin = strtotime ( $this->checkTime . '-01' );
			$begin = strtotime ( $this->checkTime . '-' . date ( 't', strtotime ( $this->checkTime ) ) );
		}
		$dayCount =  ceil ( ($end - $begin) / 3600 / 24 ) + 1;
		$firstWeek = 7 - date('w', $begin); //number of days in the first week
		$weekCount = ceil(($dayCount - $firstWeek) / 7) + 1; // plus the first week
		return ceil(($dayCount - $firstWeek) / 7) + 1; 
	}
	
	private function calculatePreSalary() {
		return $this->salaryStandard * $this->monthUnit;
	}
	public function getSalaryStandardAttr() {
		return CourseBean::$standardAttrs [$this->salaryFix+0];
	}
	public function getSalaryFixAttr(){
		$attr = $this->getSalaryStandardAttr();
		$splits=explode("/", $attr);
		if(isset($splits[0]))
				return $splits[0];
	}
}

?>